<template>
  <el-dialog title="上传素材" :visible.sync="dialogVisible" width="70%" @close="resetForm" destroy-on-close>
    <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="上级目录" prop="parentId">
        <section-parent-select v-model="form.parentId" :course-id="form.courseId" style="width: 300px;" />
      </el-form-item>

      <el-form-item label="标题" prop="sectionName">
        <el-input v-model="form.sectionName" style="width: 300px;"></el-input>
      </el-form-item>

      <el-form-item label="内容" prop="previewContent">
        <Editor :value.sync="form.previewContent"/>
      </el-form-item>     
    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="submitForm">确 定</el-button>
    </div>
  </el-dialog>
</template>
<script>
import api from '@/common/api';
import Editor from '@/components/admin/Editor';
import SectionParentSelect from '@/components/admin/SectionParentSelect';

export default {
  name: 'MdSectionDialog',
  components: {
    SectionParentSelect,
    Editor
  },
  data() {
    return {
      dialogVisible: false,
      form: {
        parentId: 0,
        sectionName: '',
        previewContent: '',
        fileUrl: '',
        fileType: 'md'
      },
      rules: {
        parentId: [
          { required: true, message: '请选择上级目录', trigger: 'change' }
        ],
        sectionName: [
          { required: true, message: '请输入标题', trigger: 'blur' },
          { min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur' }
        ],
        previewContent: [
          { required: true, message: '文件内容不能为空', trigger: 'change' }
        ]
      }
    }
    },
  methods: {
    open(data, mode = 'add') {
      if (mode === 'add') {
        const {courseId, id} = data;
        this.form = Object.assign({}, this.form, {courseId, parentId: id});
      } else {
        this.form = Object.assign({}, data)
      }
      this.dialogVisible = true
    },
    resetForm() {
      this.$refs.formRef?.resetFields()
      this.formData = {
        courseId: null,
        parentId: null,
        sectionName: '',
        fileUrl: '',    
        fileType: 'md'
      }
    },
    submitForm() {
      this.$refs.formRef.validate(async valid => {
        if (!valid) return

        try {
          this.loading = true
          const mdFormData = {
            content: this.form.previewContent,
            fileUrl: this.form.fileUrl,
          }
          const rsp = await api.admin_saveMdFile(mdFormData)
          const {link, size} = rsp.data.data

          const sectionData = Object.assign({}, this.form, {fileUrl: link, fileSize: size})
          const {data} = await api.admin_saveCourseSection(sectionData)
          
          this.$message.success('保存成功')
          this.dialogVisible = false
          this.$emit('success', data.data)
        } finally {
          this.loading = false
        }
      })
    }
  }
}
</script>
